import re
from django import forms
from django.contrib.auth import login
from django.db.models import Q
from .models import Users


class LoginForm(forms.Form):
    user_account = forms.CharField()
    password = forms.CharField(max_length=20,min_length=6,error_messages={
        'min_length':'密码长度大于6',
        'max_length':'密码长度小于20',
        'required':'密码不能为空',
    })
    remember = forms.BooleanField(required=False)

    def __init__(self,*a,**k):
        self.request = k.pop('request') #删除且返回
        super().__init__(*a,**k)



    #关联clean_定义名
    def clean_user_account(self):
        user_info = self.cleaned_data.get('user_account')
        if not user_info:
            raise forms.ValidationError('用户名不能为空')

        if not re.match(r'^1[358]\d{9}$',user_info) and (len(user_info)<5 or
        len(user_info)>20):
            raise forms.ValidationError('输入的格式错误，请重新输入！')
        return user_info

    def clean(self):
        cleaned_data = super().clean() #返回的是字典
        user_info = cleaned_data.get('user_account')
        pass_wd = cleaned_data.get('password')
        rmber = cleaned_data['remember']

        #判断是用户名还是手机号
        user_qs =Users.object.filter(Q(mobile = user_info) | Q(username = user_info))
        if user_qs:
            user= user_qs.first()
            if user.check_password(pass_wd):
                if rmber:
                    self.request.session.set_expiry(None)  #默认14天
                else:
                    self.request.session.set_expiry(0)
                login(self.request,user)
            else:
                raise forms.ValidationError('用户名或密码错误')
        else:
            raise forms.ValidationError('用户名不存在，请重新输入')

        return cleaned_data





















